package com.robusta.cityuate.objects;

import java.io.Serializable;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.robusta.cityuate.activities.UserProfileActivity;
import com.robusta.cityuate.services.GetUserService;
import com.robusta.cityuate.services.HTTPService;
import com.robusta.cityuate.services.RateUserService;
import com.robusta.cityuate.utils.ConnectionUtils;

@SuppressWarnings("serial")
public class User implements Serializable {

	/*
	 * {"user": {"first_name":"Fooooozo", "last_name":"Baravo 3aleek",
	 * "username":"shanab", "mobile_number":"01000705867", "dob":"1991-02-15",
	 * "city":"Cairo", "district":"Nasr City",
	 * "street":"Mohy El Deen Abdel Hameed St., 8th District",
	 * "building_number":"72", "zip_code":"11762", "country_id":63,
	 * "country_name":"Egypt", "country_code":"EG"}}
	 */

	public static final String USER = "user";
	public static final String RESERVER = "reserver";

	public static final String USER_EMAIL = "email";
	public static final String USER_PWD = "password";
	public static final String USER_FIRST_NAME = "first_name";
	public static final String USER_LAST_NAME = "last_name";
	public static final String USER_COUNTRY_ID = "country_id";
	public static final String USER_COUNTRY_NAME = "country_name";
	public static final String USER_COUNTRY_CODE = "country_code";
	public static final String USER_USERNAME = "username";
	public static final String USER_MOBILE_NUMBER = "mobile_number";
	public static final String USER_CITY = "city";
	public static final String USER_DISTRICT = "district";
	public static final String USER_STREET = "street";
	public static final String USER_BUILDING_NUMBER = "building_number";
	public static final String USER_ZIP_CODE = "zip_code";
	public static final String USER_DOB = "dob";
	public static final String USER_RATE = "rate";
	public static final String USER_ID = "id";
	public static final String USER_DOB_1I = "dob(1i)";
	public static final String USER_DOB_2I = "dob(2i)";
	public static final String USER_DOB_3I = "dob(3i)";
	public static final String USER_CURRENT_PWD = "current_password";
	public static final String USER_RATING = "user_rating";
	public static final String USER_RATING_VALUE = "value";

	public User(long i, String first, String last) {
	this.id=i;
	first_name=first;
	last_name=last;
	}

	public User() {
		// TODO Auto-generated constructor stub
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getFirst_name() {
		return first_name;
	}

	public void setFirst_name(String first_name) {
		this.first_name = first_name;
	}

	public String getLast_name() {
		return last_name;
	}

	public void setLast_name(String last_name) {
		this.last_name = last_name;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getMobile_number() {
		return mobile_number;
	}

	public void setMobile_number(String mobile_number) {
		this.mobile_number = mobile_number;
	}

	public String getDob() {
		return dob;
	}

	public void setDob(String dob) {
		this.dob = dob;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getDistrict() {
		return district;
	}

	public void setDistrict(String district) {
		this.district = district;
	}

	public String getStreet() {
		return street;
	}

	public void setStreet(String street) {
		this.street = street;
	}

	public String getBuilding_number() {
		return building_number;
	}

	public void setBuilding_number(String building_number) {
		this.building_number = building_number;
	}

	public String getZip_code() {
		return zip_code;
	}

	public void setZip_code(String zip_code) {
		this.zip_code = zip_code;
	}

	public int getCountry_id() {
		return country_id;
	}

	public void setCountry_id(int country_id) {
		this.country_id = country_id;
	}

	public String getCountry_name() {
		return country_name;
	}

	public void setCountry_name(String country_name) {
		this.country_name = country_name;
	}

	public String getCountry_code() {
		return country_code;
	}

	public void setCountry_code(String country_code) {
		this.country_code = country_code;
	}

	private Long id;
	private String email;
	private String first_name;
	private String last_name;
	private String username;
	private String mobile_number;
	private String dob;
	private String city;
	private String district;
	private String street;
	private String building_number;
	private String zip_code;
	private int country_id;
	private String country_name;
	private String country_code;
	private double rate;

	public double getRate() {
		return rate;
	}

	public void setRate(double rating) {
		this.rate = rating;
	}

	public static User fromJSON(JSONObject userJSON) {
		User user = new User();
		try {
			JSONObject userData;
			if (userJSON.has(RESERVER))
				userData = userJSON.getJSONObject(RESERVER);
			else
				userData = userJSON.getJSONObject(USER);
			// JSONObject userJSON = new JSONObject(jsonString);
			if (userData.has(USER_ID))
				user.id = userData.getLong(USER_ID);
			if (userData.has(USER_EMAIL))
				user.email = userData.getString(USER_EMAIL);
			user.first_name = userData.getString(USER_FIRST_NAME);
			user.last_name = userData.getString(USER_LAST_NAME);
			user.username = userData.getString(USER_USERNAME);
			user.mobile_number = userData.getString(USER_MOBILE_NUMBER);
			user.dob = userData.getString(USER_DOB);
			user.city = userData.getString(USER_CITY);
			user.district = userData.getString(USER_DISTRICT);
			user.street = userData.getString(USER_STREET);
			user.building_number = userData.getString(USER_BUILDING_NUMBER);
			user.zip_code = userData.getString(USER_ZIP_CODE);
			user.country_id = userData.getInt(USER_COUNTRY_ID);
			user.country_name = userData.getString(USER_COUNTRY_NAME);
			user.country_code = userData.getString(USER_COUNTRY_CODE);
			if (userData.has(USER_RATE))
			user.rate = userData.getDouble(USER_RATE);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return user;
	}

	/**
	 * fires the service to get current loggedin user
	 */
	public static void getCurrentUserProfile(Context mContext, long userId) {

		if (!ConnectionUtils.isNetworkAvailable(mContext))
			// No connection
			ConnectionUtils.showNoConnectionDialog(mContext);

		// Connection is available
		// start back-ground service to handle Create process
		Intent backgroud_regService = new Intent(mContext, GetUserService.class);
		backgroud_regService.setAction(UserProfileActivity.TAG);
		backgroud_regService.putExtra(HTTPService.LOGIN_RQUIRED, true);
		backgroud_regService.putExtra(User.USER_ID, userId);

		mContext.startService(backgroud_regService);
		Log.e(UserProfileActivity.TAG, "Fired GetUser service");

	}

	public void rate(Context mContext, float rating) {
		if (!ConnectionUtils.isNetworkAvailable(mContext))
			// No connection
			ConnectionUtils.showNoConnectionDialog(mContext);

		// Connection is available
		// start back-ground service to handle Create process
		Intent backgroud_regService = new Intent(mContext, RateUserService.class);
		backgroud_regService.setAction(UserProfileActivity.RATE_TAG);
		backgroud_regService.putExtra(HTTPService.LOGIN_RQUIRED, true);
		backgroud_regService.putExtra(User.USER_ID, getId());
		backgroud_regService.putExtra(User.USER_RATING_VALUE,(int)rating);

		mContext.startService(backgroud_regService);
		Log.e(UserProfileActivity.TAG, "Fired RateUser service");
		
	}

}